import { useStrict, observable, runInAction, computed, action } from 'mobx';

import Constants from '../constants/constants';
import request from '../utils/request';


useStrict(true);

class App {

  @observable me = null;
  @observable logging = false;
  @observable users = [];

  @action
  async login() {

    runInAction('开始登录', () => {
      this.logging = true;
    })
    

    // 请求登录
    const url = Constants.URL + '/user/login';
    const option = { method: 'POST' };
    const data = await request(url, option);

    // 解析响应
    runInAction('解析响应', () => {
      if (data && data.code == Constants.Code.SUCCESS) {
        this.me = data.data.item;
      }
    })

    runInAction('结束登录', () => {
      this.logging = false;
    })
  }

  @action
  logout() {
    this.me = null;
  }

  @action
  async getList() {

    // 请求列表
    const url = Constants.URL + '/user';
    const option = { method: 'GET' };
    const data = await request(url, option);

    // 解析响应
    runInAction('取得列表应答', () => {
      if (data && data.code == Constants.Code.SUCCESS) {
        this.users = data.data.items;
        this.total = data.data.page.total;
      }
    });
  }
}

export default new App();